﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;

namespace QuanLyKhachSan
{
    public partial class frmThayDoiQuiDinh : Form
    {
        public frmThayDoiQuiDinh()
        {
            InitializeComponent();
        }
        private QuyDinhDTO qDinh = new QuyDinhDTO();

        //bien luu he thong hien tai
        private System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;

        // Kiem tra du lieu nhap vao
        public bool KiemTraDuLieuNhap()
        {
            if (string.IsNullOrEmpty(txtPhuThu.Text) == true)
            {
                MessageBox.Show("Phan tram phu thu khong duoc de trong", "Thong bao ");
                txtPhuThu.Focus();
                return false;
            }
            if (DTO.QuyDinhDTO.KiemTraPhanTramPhuThu(txtPhuThu.Text) == false)
            {
                MessageBox.Show("Phan tram phu thu khong duoc nhap chu", "Thong bao ");
                txtPhuThu.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtSoKHToiDa.Text) == true)
            {
                MessageBox.Show("So khach hang toi da khong duong nhap chu ", "Thong bao ");
                txtSoKHToiDa.Focus();
                return false;
            }
            if (DTO.QuyDinhDTO.KiemTraSoKHToiDa(txtSoKHToiDa.Text) == false)
            {
                MessageBox.Show("So khach hang toi da khong duong nhap chu ", "Thong bao ");
                txtSoKHToiDa.Focus();
                return false;
            }
            return true;
        }
        private void frmThayDoiQuiDinh_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            txtPhuThu.Enabled = false;
            txtSoKHToiDa.Enabled = false;
            DataTable dt = new DataTable();
            dt = QuyDinhBUS.LayQuyDinh();
            DataRow dr = dt.Rows[0];
            txtSoKHToiDa.Text = dr[0].ToString();
            txtPhuThu.Text = (float.Parse(dr[1].ToString())*100).ToString();
            qDinh.SoKhachToiDa = int.Parse(dr[0].ToString());
            qDinh.PhuThu = (float)Math.Round(double.Parse(dr[1].ToString()),2);

            //su dung he thong en-US
            //oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            
        }
        private void btnThayDoi_Click(object sender, EventArgs e)
        {
            txtPhuThu.Enabled = true;
            txtSoKHToiDa.Enabled = true;
            btnThayDoi.Enabled = false;
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            
            if (KiemTraDuLieuNhap() == false)
            {
                //frmThayDoiQuiDinh_Load(sender, e);
                return;
            }
            else
            {               
                if (string.IsNullOrEmpty(txtSoKHToiDa.Text) == false && string.IsNullOrEmpty(txtPhuThu.Text) == false)
                {
                    DialogResult result = MessageBox.Show("Ban chac chan muon thay doi khong?", "Thong Bao", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        qDinh.SoKhachToiDa = int.Parse(txtSoKHToiDa.Text);
                        qDinh.PhuThu = (float)Math.Round(double.Parse(txtPhuThu.Text)/100, 2);

                        bool kq = QuyDinhBUS.Update(qDinh);
                        if (kq == true)
                        {
                            MessageBox.Show("Thay Doi Thanh Cong");
                            txtPhuThu.Enabled = false;
                            txtSoKHToiDa.Enabled = false;
                            btnLuu.Enabled = false;
                            btnThayDoi.Enabled = true;
                        }
                        else
                            MessageBox.Show("Thay Doi That Bai! Xin Thu Lai");
                    }   
                }
                
            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPhuThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtSoKHToiDa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void frmThayDoiQuiDinh_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        }
    }
}